package com.spic.business.contractorsync.service.impl;

import com.spic.business.contractorsync.domain.ContractorPerson;
import com.spic.business.contractorsync.mapper.ContractorPersonMapper;
import com.spic.business.contractorsync.service.IContractorPersonService;
import com.spic.common.core.domain.R;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.system.api.RemoteDeptService;
import com.spic.system.api.RemoteUserService;
import com.spic.system.api.domain.SysDept;
import com.spic.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 供应商人员同步Service业务层处理
 *
 * @author spic
 * @date 2022-06-05
 */
@Service
public class ContractorPersonServiceImpl implements IContractorPersonService {
    @Autowired
    private ContractorPersonMapper contractorPersonMapper;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private RemoteDeptService remoteDeptService;
    @Autowired
    private ThreadPoolExecutor executor;

    /**
     * 查询供应商人员同步
     *
     * @param id 供应商人员同步主键
     * @return 供应商人员同步
     */
    @Override
    public ContractorPerson selectContractorPersonById(Long id) {
        return contractorPersonMapper.selectContractorPersonById(id);
    }

    /**
     * 查询供应商人员同步列表
     *
     * @param contractorPerson 供应商人员同步
     * @return 供应商人员同步
     */
    @Override
    public List<ContractorPerson> selectContractorPersonList(ContractorPerson contractorPerson) {
        return contractorPersonMapper.selectContractorPersonList(contractorPerson);
    }

    /**
     * 新增承包商人员同步
     *
     * @param list 承包商人员同步
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertContractorPerson(List<ContractorPerson> list) {
        executor.execute(() -> {
            for (int i = 0; i < list.size(); i++) {
                ContractorPerson contractorPerson = list.get(i);
                ContractorPerson contractorPersonParam = new ContractorPerson();
                contractorPersonParam.setPersonIdcard(contractorPerson.getPersonIdcard());
                List<ContractorPerson> list1 = selectContractorPersonList(contractorPersonParam);
                if (list1.size() > 0) {
                    continue;
                }
                contractorPerson.setSyncHK("N");
                contractorPerson.setAccessAuth("N");
                contractorPerson.setSyncTime(new Date());
                contractorPersonMapper.insertContractorPerson(contractorPerson);
            }
        });
        return AjaxResult.success();
    }

    /**
     *  承包商数据入库
     */
    @Override
    public void contractorPersistent() {

        ContractorPerson contractorParam = new ContractorPerson();
        contractorParam.setSyncHK("N");
        List<ContractorPerson> list = selectContractorPersonList(contractorParam);
        for (int i = 0; i < list.size(); i++) {
            ContractorPerson contractorPerson = list.get(i);
            /*
                检查部门是否存在(新建部门)
                如果存在则使用dept_id,没有则在承包商部门下新建部门
            */
            SysDept sysDept = new SysDept();
            sysDept.setParentId(280L);
            sysDept.setDeptName(contractorPerson.getContractorName());
            sysDept.setOrderNum("0");
            sysDept.setStatus("0");
            R<Long> sysDeptR = remoteDeptService.addDept(sysDept);

            //{"code":200,"data":1383}
            SysUser user = new SysUser();
            //承包商
            user.setDeptId(sysDeptR.getData());
            user.setNickName(contractorPerson.getPersonName());
            user.setPassword("Long189%");
            //普通员工
            user.setPostIds(new Long[]{284L});
            user.setRemark("来源自承包商系统同步");
            //普通员工
            user.setRoleIds(new Long[]{10L});
            user.setSex("0");
            user.setStatus("0");
            user.setUserName(contractorPerson.getPersonIdcard());
            /*
             * {"code":500,"msg":"新增用户'12345678'失败，登录账号已存在"}
             * {"code":200,"msg":"操作成功"}
             */
            remoteUserService.addUser(user);
        }
    }

    /**
     * 修改供应商人员同步
     *
     * @param contractorPerson 供应商人员同步
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int updateContractorPerson(ContractorPerson contractorPerson) {
        return contractorPersonMapper.updateContractorPerson(contractorPerson);
    }

    /**
     * 批量删除供应商人员同步
     *
     * @param ids 需要删除的供应商人员同步主键
     * @return 结果
     */
    @Override
    public int deleteContractorPersonByIds(Long[] ids) {
        return contractorPersonMapper.deleteContractorPersonByIds(ids);
    }

}
